//
//  CTThirdPartyLoginView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 hanwei. All rights reserved.
//

#import "CTThirdPartyLoginView.h"
#import "CTAppLanguage.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "UIViewAdditions.h"
#import "CTLoginView_Def.h"
#import "CTAppInfo.h"
#import <QZoneConnection/ISSQZoneApp.h>
#import <ShareSDK/ShareSDK.h>

#define CT_THIRDPARTYLOGINVIEW_BUTTON_TAG_SINA   1000
#define CT_THIRDPARTYLOGINVIEW_BUTTON_TAG_QQ     1001
#define CT_THIRDPARTYLOGINVIEW_BUTTON_TAG_WECHAT 1002

@interface CTThirdPartyLoginView()
{
    UIView  *thirdPartyView;
}
@end
@implementation CTThirdPartyLoginView
@synthesize delegate;
- (void)dealloc
{
    delegate = nil;
    [thirdPartyView removeFromSuperview];
    thirdPartyView = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTThirdPartyLoginView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        self.frame = CGRectMake(0,0.7196*CT_HEADER_SCREEN_HEIGHT,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT*(1-0.7196));
        
        [self createThirdPartyLoginView];
    }
    return self;
}
- (void)createThirdPartyLoginView
{
    //其它方式登录
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-305)/2, 0, 305, CT_HEADER_SCREEN_HEIGHT*0.2743)];
    [self addSubview:view];
    
    thirdPartyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, view.width, view.height-30)];
    thirdPartyView.hidden = YES;
    [view addSubview:thirdPartyView];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.width, 15)];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"thirdPartyLogInView_titleLabelText" withValue:nil withTable:@"Language"];
    titleLabel.font = [UIFont fontWithName:nil size:15];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    [thirdPartyView addSubview:titleLabel];
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 7.5,(view.width-60-31)/2, 0.5)];
    leftImageView.image = [UIImage imageNamed:@"login_line_username"];
    [thirdPartyView addSubview:leftImageView];
    
    UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake( 32+leftImageView.width+60, 7.5, leftImageView.width, 0.5)];
    rightImageView.image = [UIImage imageNamed:@"login_line_username"];
    [thirdPartyView addSubview:rightImageView];
    NSArray *buttonImageStr = [NSArray arrayWithObjects:@"sina",@"qq",@"wechat", nil];
    NSArray *buttonImageClickStr = [NSArray arrayWithObjects:@"sina_click",@"qq_click",@"wechat_click", nil];
    for (int i = 0; i < 3; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(45+i*(45+40), rightImageView.bottom+0.0629*CT_HEADER_SCREEN_HEIGHT, 40, 40);
        button.tag = 1000+i;
        [button setImage:[UIImage imageNamed:buttonImageStr[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:buttonImageClickStr[i]] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(thirdPartyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [thirdPartyView addSubview:button];
    }
    NSString *proStr = [CTAppLanguage localizedStringForKey:@"thirdPartyLoginView_protocol_label_text" withValue:nil withTable:@"Language"];
    UILabel *protocolLabel = [[UILabel alloc]initWithFrame:CGRectMake(58, view.bottom-30, proStr.length*10, 10)];
    protocolLabel.text = proStr;
    protocolLabel.textAlignment = NSTextAlignmentCenter;
    protocolLabel.textColor = CT_HEADER_GREEN_COLOR;
    protocolLabel.font = [UIFont fontWithName:nil size:10];
    [view addSubview:protocolLabel];
    
    NSString *proLableStr = [CTAppLanguage localizedStringForKey:@"thirdPartyLoginView_protocolLabel_text" withValue:nil withTable:@"Language"];
    UILabel *protocolBtnLabel = [[UILabel alloc]initWithFrame:CGRectMake(protocolLabel.right+5, protocolLabel.origin.y, proLableStr.length*10, 10)];
    protocolBtnLabel.text = proLableStr;
    protocolBtnLabel.textAlignment = NSTextAlignmentLeft;
    protocolBtnLabel.textColor = CT_HEADER_GREEN_COLOR;
    protocolBtnLabel.font = [UIFont fontWithName:nil size:10];
    [view addSubview:protocolBtnLabel];
    
    UIImageView *proImageView = [[UIImageView alloc]initWithFrame:CGRectMake(protocolBtnLabel.origin.x, protocolBtnLabel.bottom+2, protocolBtnLabel.width-40, 0.5)];
    proImageView.image = [UIImage imageNamed:@"login_line_username"];
    [view addSubview:proImageView];
    
    
    UIButton *protocolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    protocolBtn.frame = CGRectMake(protocolLabel.right+5, protocolLabel.origin.y, protocolBtnLabel.width, 20);
    [protocolBtn addTarget:self action:@selector(protoclBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:protocolBtn];
    
    
    if ([CTAppInfo isAppOnline] == YES) {
        thirdPartyView.hidden = NO;
    }else{
        thirdPartyView.hidden = YES;
    }
//    id<ISSQZoneApp> app =(id<ISSQZoneApp>)[ShareSDK getClientWithType:ShareTypeQQSpace];
//    [app setIsAllowWebAuthorize:YES];
//     thirdPartyView.hidden = NO;
    
}
//用户协议
- (void)protoclBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(proButtonAction:)])
    {
        [self.delegate proButtonAction:self];
    }
}
- (void)thirdPartyButtonAction:(UIButton *) sender
{
    switch (sender.tag) {
        case CT_THIRDPARTYLOGINVIEW_BUTTON_TAG_SINA:
            if ([self.delegate respondsToSelector:@selector(sinaLoginButton:)])
            {
                [self.delegate sinaLoginButton:self];
            }
            break;
        case CT_THIRDPARTYLOGINVIEW_BUTTON_TAG_QQ:
            if ([self.delegate respondsToSelector:@selector(qqLoginButton:)])
            {
                [self.delegate qqLoginButton:self];
            }
            break;
        case CT_THIRDPARTYLOGINVIEW_BUTTON_TAG_WECHAT:
            if ([self.delegate respondsToSelector:@selector(weChatLoginButton:)])
            {
                [self.delegate weChatLoginButton:self];
            }
            break;
            
        default:
            break;
    }
}


@end
